package com.app.controller;

import com.app.model.ComRecruitersModel;
import com.app.model.JobFairCompanyModel;
import com.app.model.JobFairSceneModel;
import com.app.model.UserModel;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@Controller
@NoArgsConstructor
@AllArgsConstructor
@Data
public class JobFairSceneController {


    @Autowired
    JobFairSceneService jobFairSceneService;
    @Autowired
    UserService userService;

    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobFairCompanyService jobFairCompanyService;
    @Autowired
    JobFairCompanyPostService jobFairCompanyPostService;


    @RequestMapping(value = "findAllById", method = RequestMethod.GET)
    public Result<Object> findAllById(int id) {
        JobFairSceneModel scenes = jobFairSceneService.findAllById(id);
        if(scenes!=null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Timestamp data = Timestamp.valueOf(scenes.getEventTimeStart());
            Timestamp data2 = Timestamp.valueOf(scenes.getEventTimeEnd());
            String starTime = sdf.format(data);
            String endTime = sdf.format(data2);
            scenes.setEventTimeStart(starTime);
            scenes.setEventTimeEnd(endTime);
            scenes.setQyCount(jobFairCompanyService.countByCid(scenes.getId()));
            int countPost = jobFairCompanyPostService.countBySid(scenes.getId());
            scenes.setZwCount(countPost);
            scenes.setBrowseNumber(scenes.getBrowseNumber() + 1);
            scenes.setResumeCount(jobFairCompanyService.countByResume(id));
            jobFairSceneService.update(scenes);
            if(data.getTime() > CommonHelp.getNowTimestamp().getTime())
            {
                scenes.setState(1);
            }
            if(data.getTime() < CommonHelp.getNowTimestamp().getTime()&&data2.getTime()>CommonHelp.getNowTimestamp().getTime())
            {
                scenes.setState(2);
            }
            if(data2.getTime()<CommonHelp.getNowTimestamp().getTime())
            {
                scenes.setState(3);
            }
        }
       return Result.success(scenes);
    }

    @GetMapping("/findAllByCrtTime")
    public Result<Object> getWorkExp(HttpServletRequest request) {
        String sceneType = request.getParameter("sceneType");
        JobFairSceneModel model = new JobFairSceneModel();
        if (sceneType != null && sceneType != "") {
            model.setSceneType(sceneType);
        }
        List<JobFairSceneModel> scenes = jobFairSceneService.findAllByCrtTime(model);
        if(scenes!=null)
        {
            for (int i = 0; i < scenes.size(); i++) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd");
                SimpleDateFormat simpleDateFormatFair = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Timestamp data = Timestamp.valueOf(scenes.get(i).getCrtTime());
                Timestamp data1 = Timestamp.valueOf(scenes.get(i).getCrtTime());
                Timestamp data3 = Timestamp.valueOf(scenes.get(i).getEventTimeStart());//开始时间
                Timestamp data4 = Timestamp.valueOf(scenes.get(i).getEventTimeEnd());//结束时间
                String currentSimpleDateFormat = simpleDateFormat.format(data);
                String currentSimpleDateFormat1 = simpleDateFormat1.format(data1);
                String currentSimpleDateFormat3 = simpleDateFormatFair.format(data3);
                String currentSimpleDateFormat4 = simpleDateFormatFair.format(data4);
                scenes.get(i).setBnumber(currentSimpleDateFormat);
                scenes.get(i).setNumber(currentSimpleDateFormat1);
                scenes.get(i).setEventTimeStart(currentSimpleDateFormat3);
                scenes.get(i).setEventTimeEnd(currentSimpleDateFormat4);
                if(data3.getTime() > CommonHelp.getNowTimestamp().getTime())
                {
                    scenes.get(i).setState(1);
                }
                if(data3.getTime() < CommonHelp.getNowTimestamp().getTime()&&data4.getTime()>CommonHelp.getNowTimestamp().getTime())
                {
                    scenes.get(i).setState(2);
                }
                if(data4.getTime()<CommonHelp.getNowTimestamp().getTime())
                {
                    scenes.get(i).setState(3);
                }
            }
            for (int i = 0; i < scenes.size(); i++) {
                scenes.get(i).setQyCount(jobFairCompanyService.countBySid(scenes.get(i).getId()));
                List<JobFairCompanyModel> fairCompanyList = jobFairCompanyService.getModelBySid(scenes.get(i).getId());
                int countPost = 0;
                if(fairCompanyList!=null)
                {
                    for (int j = 0; j < fairCompanyList.size(); j++) {
                        countPost += jobFairCompanyPostService.countBySid(scenes.get(i).getId());
                    }
                }
                scenes.get(i).setZwCount(countPost);
            }
        }

       return Result.success(scenes);
    }

    @GetMapping("/getMyJobFairs")
    public Result<Object> getMyJobFairs(HttpServletRequest request,Integer page, Integer limit) {

        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405, "用户不存在");

        }
        JobFairCompanyModel model = new JobFairCompanyModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        ComRecruitersModel com = comRecruitersService.getComRecruitersById(user.getRId());
        model.setCid(com.getComId());
        List<JobFairCompanyModel> scenes = jobFairCompanyService.getMyJobFair(model);
        for (int i = 0; i < scenes.size(); i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd");
            SimpleDateFormat simpleDateFormatFair = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Timestamp data = Timestamp.valueOf(scenes.get(i).getCrtTime());
            Timestamp data1 = Timestamp.valueOf(scenes.get(i).getCrtTime());
            Timestamp data3 = Timestamp.valueOf(scenes.get(i).getEventTimeStart());//开始时间
            Timestamp data4 = Timestamp.valueOf(scenes.get(i).getEventTimeEnd());//结束时间
            String currentSimpleDateFormat = simpleDateFormat.format(data);
            String currentSimpleDateFormat1 = simpleDateFormat1.format(data1);
            String currentSimpleDateFormat3 = simpleDateFormatFair.format(data3);
            String currentSimpleDateFormat4 = simpleDateFormatFair.format(data4);
            scenes.get(i).setBnumber(currentSimpleDateFormat);
            scenes.get(i).setNumber(currentSimpleDateFormat1);
            scenes.get(i).setEventTimeStart(currentSimpleDateFormat3);
            scenes.get(i).setEventTimeEnd(currentSimpleDateFormat4);
            if(scenes.get(i).getAuditState()==1)
            {
                scenes.get(i).setAuditStateText("审核通过");
            }
            if(scenes.get(i).getAuditState()==2)
            {
                scenes.get(i).setAuditStateText("审核未通过");
            }
            if(scenes.get(i).getAuditState()==3||scenes.get(i).getAuditState()==0)
            {
                scenes.get(i).setAuditStateText("待审核");
            }

            if(data3.getTime() > CommonHelp.getNowTimestamp().getTime())
            {
                scenes.get(i).setStateText("预约中");
            }
            if(data3.getTime() < CommonHelp.getNowTimestamp().getTime()&&data4.getTime()>CommonHelp.getNowTimestamp().getTime())
            {
                scenes.get(i).setStateText("进行中");
            }
            if(data4.getTime()<CommonHelp.getNowTimestamp().getTime())
            {
                scenes.get(i).setStateText("已结束");
            }

        }
        return Result.success(scenes);
    }



}
